<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix='sec' uri='http://www.springframework.org/security/tags'%>
<head>
	<script type="text/javascript" src="scripts/boot.js"></script>
	<style type="text/css">
    .errorText
    {
        color:red;
    }
    </style>
</head>

<body>
	<form id="form1" method="post">
		<p style="margin-left: 20px;">
			<label>部门名称：</label>
			<input id="textbox1"  name="name" class="mini-textbox" onblur="nameOnly();" required="true" errorMode="none" onvalidation="checkValidation" requiredErrorText="部门名称不能为空" required="true" vtype="rangeLength:0,50" style="width: 150px;"/>
			<span style="margin-left:60px;" id="name_error" class="errorText" ></span>
		</p>
		<p style="margin-left: 20px;">
			<label>部门类型：</label>
			<input id="combo1" name="deptType" class="mini-combobox" style="width:150px;" textField="keyValue" valueField="keyName" emptyText="请选择..."
    				url="dictionary_key_val_view.do?typeName=DEPT_TYPE" allowInput="false" showNullItem="true" nullItemText="请选择..."/> 
		</p>
		<p style="margin-left: 20px;">
			<label>请输入描述信息(备注)：</label>
			<input id="textarea1" name="description" class="mini-textarea" style="width: 212px; "/>
		</p>
		<input id="text1"  name="id" class="mini-hidden" required="true" />
		<input id="text2"  name="nameHidden" class="mini-hidden" required="true" />
	</form>
    <p style="margin-left: 35px;">
    	<a class="mini-button" onclick="onOk" style="width:60px;margin-right:20px;">确定</a>
    	<a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>
    </p>
</body>

<script type="text/javascript">
	mini.parse();
	var form = new mini.Form("form1");
	
	function SaveData() {
        var o = form.getData(true);            
		
        form.validate();
        if (form.isValid() == false || nameOnly() == false) return;
        $.ajax({
            url: "medical_deptedit_save.do",
			type: 'post',
			dataType: 'json',
            data: o,
            cache: false,
            success: function (text) {
            	if(text.success == "true"){
	            	window.CloseOwnerWindow("succ");
	            }else if(text.success == "false"){
	            	mini.alert("id不能为空！");
	            }else {
	            	mini.alert("数据错误");
	            }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
                CloseWindow();
            }
        });
    }
	
	function onOk(e) {
        SaveData();
    }
	
	function SetData(data){
	     data = mini.clone(data);
	     form.setData(data.row);
	     mini.getbyName('nameHidden').setValue(data.row.name);
	}
	
	function CloseWindow(action) {
        if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
        else window.close();            
    }
	
	function onCancel(e) {
        CloseWindow("cancel");
    }

   	function updateError(e) {
	    var id =e.sender.name + "_error";
	    var el = document.getElementById(id);
	    if (el) {
	        el.innerHTML =e.errorText;
	    }
   	}
   
   	function checkValidation(e) {                  
  		updateError(e);
   	}
	
   	function nameOnly(){
   		var name = mini.getbyName('name').getValue();
   		var nameHidden = mini.getbyName('nameHidden').getValue();
   		var flag = true;
   		if(name.length > 0 && name != nameHidden){
   	  	    $.ajax({
   	  	    	url: "medical_dept_include.do?name=" + name,
   	  	    	dataType: 'json',
   	  	        cache: false,
   		    	async: false,
   	 	        success: function (text) {
   	 	            if(text.success){
   	 	            	flag = true;
   	 	            }else{
   	 	            	mini.alert("部门名称重复");
   	 	            	flag = false;
   	 	            }
   	 	        }
   	  	    });
   		}
    	return flag;
   	}
   	
</script>